/*
 *  This script, in JQUERY, is only for the subject administration page
 */

var currentUuid = null; // will contain the uuid of the subject we want to modify
var currentFatherTitle = null;

$(document).ready(function() {
    // when clicking on the link to show the form
    $("a.modifySubjectLink").live("click",function(){
        var classes = $(this).attr("class").split(" "); // the classes, the one containing the uuid is the last one
        var newUuid = getUuidPart(classes[classes.length-1]);
        var updated = false;
        
        if ($("div.adminFormSubject_"+currentUuid).is(":visible")) {
            $("div.adminFormSubject_"+currentUuid).hide(300);
            updated = true;
        }
        
        if (currentUuid != newUuid) {
            if ($("div.adminFormSubject_"+newUuid).is(":visible")) {
                $("div.adminFormSubject_"+newUuid).hide(300);
            }
            else {
                $("div.adminFormSubject_"+newUuid).show(300);
            }
            currentUuid = newUuid;
            $("div.adminFormSubject_"+currentUuid).show(300);
            currentFatherTitle = $("span.subjectFatherTitle_"+currentUuid).text();
        }
        else {
            if (!updated && !$("div.adminFormSubject_"+currentUuid).is(":visible")) {
                $("div.adminFormSubject_"+currentUuid).show(300);
                updated = true;
            }
        }
        cancelFatherSelection();
    });
    // link to modify the father
    $("a.chooseSubjectFather").live("click",function(){
        $("a.chooseSubjectFatherLink").show();
        $("a.chooseSubjectFatherLink_"+currentUuid).hide(); // you mustn't choose the subject itself as his own father, so hide his choose link
        $("span.pickFatherExplication").show();
        $("a.chooseSubjectFather").hide();
        $("a.cancelSubjectFather").show();
    });
    // link to cancel the modification of the father
    // doesnt actually cancel it, just cancel the selection
    $("a.cancelSubjectFather").live("click",function(){
        cancelFatherSelection();
    });
    // when choosing a new father
    // we need to update the hidden input with the father uuid and the text containing the title of the father
    $("a.chooseSubjectFatherLink").live("click",function(){
        var classes = $(this).attr("class").split(" "); // the classes, the one containing the uuid is the last one
        var newUuid = getUuidPart(classes[classes.length-1]);
        $("span#adminSubjectFatherUuid_"+currentUuid+" input").each(function(index) {
            $(this).attr("value",newUuid);
        });
        currentFatherTitle = $("span#subjectTitle_"+newUuid).text();
        $("span.subjectFatherTitle_"+currentUuid).html(currentFatherTitle);
        cancelFatherSelection();
    });
});
/**
 *  Cancel the father selection by hind links etc..
 */
function cancelFatherSelection() {
    $("a.chooseSubjectFatherLink").hide();
    $("a.chooseSubjectFather").show();
    $("span.pickFatherExplication").hide();
    $("a.cancelSubjectFather").hide();
}


/**
 * Return the Uuid part of the class/id
 * Assuming that the Uuid is after the "_" in the string
 */
function getUuidPart(str) {
    return str.toString().split("_")[1];
}